﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.EditPages
{
    public partial class EditRole : BasePage
    {
        POSSystem.BLL.Roles role;
        int roleId;
        string mode;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (Request.QueryString[KEY] == null)
            {
                CloseDlg(0);
                return;
            }

            if (Request.QueryString[MODE] == null)
            {
                CloseDlg(0);
                return;
            }

            mode = Request.QueryString[MODE];

            if (Request.QueryString[ID] == null && (mode == EDIT_MODE || mode == VIEW_MODE))
            {
                CloseDlg(0);
                return;
            }

            role = (POSSystem.BLL.Roles)Session[Request.QueryString[KEY]];
            string oldFilter = role.Filter;

            roleId = Request.QueryString[ID] != null ? int.Parse(Request.QueryString[ID]) : 0;
            if (mode != ADD_MODE)
                role.Filter = POSSystem.BLL.Roles.ColumnNames.ID + "=" + roleId.ToString();
            else
                btnSave.Enabled = false;

            if (!IsPostBack && role.RowCount > 0 && mode != ADD_MODE)
                editRole.RoleName = role.RoleName;

            role.Filter = oldFilter;
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            if (role != null)
            {
                Save();
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true);
            }
        }

        private void Save()
        {
            string oldFilter = role.Filter;
            if (mode == ADD_MODE)
            {
                if (POSSystem.BLL.Roles.CheckRoleExisted(editRole.RoleName, null))
                {
                    ((Popup)Master).ShowErrorMessage("Role name existed. Please choose another name.");
                    return;
                }
                role.AddNew();
                role.BranchID = Branches.CurrentBranchID;
                role.Deleted = false;
            }
            else
            {
                if (POSSystem.BLL.Roles.CheckRoleExisted(editRole.RoleName, roleId))
                {
                    ((Popup)Master).ShowErrorMessage("Role name existed. Please choose another name.");
                    return;
                }
                role.Filter = Categories.ColumnNames.ID + "=" + roleId;
            }

            role.RoleName = editRole.RoleName;
            role.LastModifiedByUser = SessionUser.ID;
            role.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
            role.Products = false;
            role.Purchase = false;
            role.Repair = false;
            role.Report = false;
            role.Sales = false;
            role.Customers = false;
            role.Suppliers = false;
            role.Admin = false;
            role.Contracts = false;
            role.SystemAdmin = false;
            role.EditSales = false;
            role.Save();
            role.Filter = oldFilter;
            ((Popup)Master).ShowMessage("Saved");
        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            if (role != null)
            {
                Save();
                CloseDlg(1);
            }
        }
    }
}
